package com.vipsoft.ws.client.service.impl;

import com.vipsoft.ws.client.interceptor.AuthInterceptor;
import com.vipsoft.ws.client.service.IDemoService;
import org.apache.cxf.binding.soap.interceptor.SoapHeaderInterceptor;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.URI;
import java.net.URL;

@Service
public class DemoServiceImpl implements IDemoService {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${vipsoft.webservice.url}")
    private String wsdlUrl;


    /**
     * 通过 apache-cxf-3.3.1 生成的类文件调用
     *
     * @param param
     * @return
     */
    @Override
    public String soapTest(String param) {
        try {
            //调用简单，但要注意识别好类【生成的文件，不要随意放，否则可能会引起错误】
            //com.vipsoft.ws.service.DemoWebService_Service service = new com.vipsoft.ws.service.DemoWebService_Service();
            com.vipsoft.ws.service.DemoWebService_Service service = new com.vipsoft.ws.service.DemoWebService_Service(new URL(wsdlUrl)); //如果地址换了，使用带参数 new
            com.vipsoft.ws.service.DemoWebService serviceSoap = service.getDemoWebServiceImplPort();
            //添加 Header
            Client client = ClientProxy.getClient(serviceSoap);
            client.getOutInterceptors().add(new AuthInterceptor());

            return serviceSoap.sayHello(param);
        } catch (Exception ex) {
            return ex.getMessage();
        }
    }

    /**
     * 动态调用 WebService
     *
     * @param param
     * @return
     */
    @Override
    public String soapDynamicTest(String param) {
        //创建动态客户端
        JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
//        Client client = factory.createClient("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl"); //测试用
        Client client = factory.createClient(wsdlUrl);
        client.getOutInterceptors().add(new AuthInterceptor());  // 需要密码的情况需要加上用户名和密码
        HTTPConduit conduit = (HTTPConduit) client.getConduit();
        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
        httpClientPolicy.setConnectionTimeout(2000);  //连接超时
        httpClientPolicy.setAllowChunking(false);    //取消块编码
        httpClientPolicy.setReceiveTimeout(120000);     //响应超时
        conduit.setClient(httpClientPolicy);

        try {

            //Object[] objects = new Object[0];
            // invoke("方法名",参数1,参数2,参数3....);
            // objects = client.invoke("getCountryCityByIp", ip);
            // return ((ArrayOfString) objects[0]).getString().toString();

            Object[] objects = client.invoke("sayHello", param);
            logger.info("返回数据:" + objects[0]);
            return objects[0].toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
